<template>
  <div class="manual-attendance">
    <el-card class="attendance-card">
      <template #header>
        <div class="card-header">
          <span class="header-title">员工异常考勤审批</span>
        </div>
      </template>

      <!-- 搜索区域 -->
      <div class="search-area">
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="员工编号">
            <el-input 
              v-model="searchForm.employeeNo" 
              placeholder="请输入员工编号" 
              clearable
              @keyup.enter="handleSearch"
              class="search-input"
            />
          </el-form-item>
          <el-form-item label="员工姓名">
            <el-input 
              v-model="searchForm.name" 
              placeholder="请输入员工姓名" 
              clearable
              @keyup.enter="handleSearch"
              class="search-input"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch" class="search-btn">
              <el-icon><Search /></el-icon>查询
            </el-button>
            <el-button @click="resetSearch" class="reset-btn">
              <el-icon><RefreshRight /></el-icon>重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 异常记录列表 -->
      <el-table 
        :data="tableData" 
        border 
        style="width: 100%" 
        v-loading="loading"
        class="attendance-table"
        :header-cell-style="{
          background: '#f8faff',
          color: '#303133',
          fontWeight: '600',
          fontSize: '14px'
        }"
      >
        <el-table-column prop="employeeNo" label="员工编号" width="120" />
        <el-table-column prop="employeeName" label="员工姓名" width="120" />
        <el-table-column prop="date" label="日期" width="120" />
        <el-table-column prop="checkTime" label="打卡时间" width="180" />
        <el-table-column prop="checkTypeText" label="打卡类型" width="100">
          <template #default="scope">
            <el-tag effect="light" type="primary">
              {{ scope.row.checkTypeText }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="checkTypeDesc" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.checkStatus)" effect="light">
              {{ scope.row.checkTypeDesc }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="reason" label="异常原因" min-width="150" show-overflow-tooltip />
        <el-table-column prop="explanation" label="申诉说明" min-width="150" show-overflow-tooltip />
        <el-table-column prop="submittedToAdmin" label="申诉状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.submittedToAdmin ? 'warning' : 'info'" effect="light">
              {{ scope.row.submittedToAdmin ? '已申诉' : '未申诉' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="adminComment" label="处理备注" min-width="150" show-overflow-tooltip />
        <el-table-column prop="processTime" label="处理时间" width="180" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="scope">
            <el-button 
              link 
              type="primary" 
              @click="handleProcess(scope.row)"
              :disabled="scope.row.processedByAdmin || !scope.row.submittedToAdmin"
              class="action-btn"
            >
              处理申诉
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          class="custom-pagination"
        />
      </div>
    </el-card>

    <!-- 处理申诉对话框 -->
    <el-dialog 
      v-model="dialogVisible" 
      title="处理申诉" 
      width="500px"
      class="process-dialog"
    >
      <div class="change-preview" v-if="currentRecord">
        <h4>申诉信息：</h4>
        <p><strong>员工编号：</strong>{{ currentRecord.employeeNo }}</p>
        <p><strong>员工姓名：</strong>{{ currentRecord.employeeName }}</p>
        <p><strong>异常日期：</strong>{{ currentRecord.date }}</p>
        <p><strong>打卡时间：</strong>{{ currentRecord.checkTime }}</p>
        <p><strong>申诉说明：</strong>{{ currentRecord.explanation }}</p>
      </div>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="处理结果" prop="status">
          <el-radio-group v-model="form.status" class="status-group">
            <el-radio :label="1">确认有效</el-radio>
            <el-radio :label="2">驳回申诉</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理备注" prop="remark">
          <el-input 
            v-model="form.remark" 
            type="textarea" 
            rows="3" 
            placeholder="请输入处理备注"
            class="form-textarea"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">
            <el-icon><Close /></el-icon>取消
          </el-button>
          <el-button type="primary" @click="submitProcess" class="submit-btn">
            <el-icon><Check /></el-icon>确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getExceptionList, updateExceptionStatus } from '@/api/admin/ManualAttendance'
import { 
  Search, 
  RefreshRight, 
  Check, 
  Close,
  WarningFilled,
  CircleCheckFilled
} from '@element-plus/icons-vue'

// 加载状态
const loading = ref(false)

// 搜索表单
const searchForm = reactive({
  employeeNo: '',
  name: ''
})

// 表格数据
const tableData = ref([])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取当前管理员编号
const adminNo = localStorage.getItem('employeeNo')

// 对话框相关
const dialogVisible = ref(false)
const formRef = ref(null)
const currentRecord = ref({})
const form = reactive({
  recordId: null,
  status: 1,
  remark: '',
  adminNo: adminNo
})

// 表单验证规则
const rules = {
  status: [{ required: true, message: '请选择处理结果', trigger: 'change' }],
  remark: [
    { required: true, message: '请输入处理备注', trigger: 'blur' },
    { min: 2, max: 200, message: '长度在 2 到 200 个字符', trigger: 'blur' },
  ],
}

// 获取状态对应的类型
const getStatusType = (status) => {
  const typeMap = {
    1: 'success', // 正常
    2: 'warning', // 迟到
    3: 'warning', // 早退
    4: 'danger',  // 缺勤
  }
  return typeMap[status] || 'info'
}

// 获取异常记录列表
const fetchExceptionList = async () => {
  if (!adminNo) {
    ElMessage.error('获取管理员信息失败')
    return
  }

  loading.value = true
  try {
    const params = {
      adminNo,
      employeeNo: searchForm.employeeNo || '',
      name: searchForm.name || '',
      current: currentPage.value,
      size: pageSize.value
    }
    const res = await getExceptionList(params)
    if (res.data) {
      tableData.value = res.data.records || res.data
      total.value = res.data.total || (res.data.length || 0)
    }
  } catch (error) {
    console.error('获取异常记录失败：', error)
    ElMessage.error('获取异常记录失败')
  } finally {
    loading.value = false
  }
}

// 搜索处理
const handleSearch = () => {
  currentPage.value = 1
  fetchExceptionList()
}

// 重置搜索
const resetSearch = () => {
  searchForm.employeeNo = ''
  searchForm.name = ''
  currentPage.value = 1
  fetchExceptionList()
}

// 处理申诉
const handleProcess = (row) => {
  currentRecord.value = row
  form.recordId = row.id
  form.status = 1
  form.remark = ''
  form.adminNo = adminNo
  dialogVisible.value = true
}

// 提交处理结果
const submitProcess = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    const res = await updateExceptionStatus({
      recordId: form.recordId,
      status: form.status,
      remark: form.remark,
      adminNo: form.adminNo
    })
    
    if (res.data) {
      ElMessage.success('处理成功')
      dialogVisible.value = false
      fetchExceptionList()
    } else {
      ElMessage.error(res.data?.message || '处理失败')
    }
  } catch (error) {
    console.error('处理申诉失败：', error)
    ElMessage.error('处理失败')
  }
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchExceptionList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchExceptionList()
}

// 页面加载时获取数据
onMounted(() => {
  fetchExceptionList()
})
</script>

<style scoped lang="scss">
.manual-attendance {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  
  .attendance-card {
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
    overflow: hidden;
    
    &:hover {
      box-shadow: 0 12px 32px rgba(24, 144, 255, 0.1);
      transform: translateY(-2px);
    }
  }
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #f0f0f0;
    
    .header-title {
      font-size: 18px;
      font-weight: 600;
      color: #303133;
      position: relative;
      padding-left: 12px;
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 4px;
        height: 18px;
        background: #1890ff;
        border-radius: 2px;
      }
    }
  }
  
  .search-area {
    margin-bottom: 20px;
    padding: 20px;
    background-color: #f8faff;
    border-radius: 8px;
    
    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #606266;
    }
    
    .search-input {
      width: 220px;
      
      :deep(.el-input__wrapper) {
        border-radius: 8px;
        box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1) inset;
        
        &:hover, &.is-focus {
          box-shadow: 0 0 0 1px #1890ff inset;
        }
      }
    }
    
    .search-btn {
      background: linear-gradient(135deg, #1890ff, #36a6ff);
      border: none;
      border-radius: 20px;
      padding: 8px 16px;
      transition: all 0.3s ease;
      box-shadow: 0 2px 6px rgba(24, 144, 255, 0.2);
      margin-right: 10px;
      
      .el-icon {
        margin-right: 4px;
      }
      
      &:hover {
        background: linear-gradient(135deg, #40a9ff, #5cbbff);
        box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
        transform: translateY(-2px);
      }
      
      &:active {
        background: linear-gradient(135deg, #096dd9, #1890ff);
        transform: translateY(0);
      }
    }
    
    .reset-btn {
      background: #f5f7fa;
      color: #606266;
      border: none;
      border-radius: 20px;
      padding: 8px 16px;
      transition: all 0.3s ease;
      
      .el-icon {
        margin-right: 4px;
      }
      
      &:hover {
        background: #e6f7ff;
        color: #1890ff;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        transform: translateY(-2px);
      }
      
      &:active {
        background: #dcf0fd;
        transform: translateY(0);
      }
    }
  }
  
  .attendance-table {
    margin: 16px 0;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
    
    :deep(.el-table__row) {
      transition: all 0.3s;
      
      &:hover {
        background-color: #f8faff !important;
      }
    }
    
    :deep(.el-table__cell) {
      padding: 10px 0;
    }
    
    :deep(.el-tag) {
      padding: 0 10px;
      height: 28px;
      line-height: 28px;
      border-radius: 4px;
      font-weight: 500;
      font-size: 12px;
      
      &.el-tag--success {
        background-color: rgba(82, 196, 26, 0.08);
        border-color: rgba(82, 196, 26, 0.2);
        color: #52c41a;
      }
      
      &.el-tag--info {
        background-color: rgba(144, 147, 153, 0.08);
        border-color: rgba(144, 147, 153, 0.2);
        color: #909399;
      }
      
      &.el-tag--warning {
        background-color: rgba(250, 173, 20, 0.08);
        border-color: rgba(250, 173, 20, 0.2);
        color: #faad14;
      }
      
      &.el-tag--danger {
        background-color: rgba(245, 34, 45, 0.08);
        border-color: rgba(245, 34, 45, 0.2);
        color: #f5222d;
      }
      
      &.el-tag--primary {
        background-color: rgba(24, 144, 255, 0.08);
        border-color: rgba(24, 144, 255, 0.2);
        color: #1890ff;
      }
    }
    
    .action-btn {
      font-size: 13px;
      font-weight: 500;
      transition: all 0.3s;
      
      &:hover {
        transform: translateY(-2px);
        opacity: 0.8;
      }
      
      &:active {
        color: #096dd9;
      }
    }
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    padding: 10px 0;
    
    :deep(.custom-pagination) {
      padding: 0;
      
      .el-pagination__total {
        margin-right: 16px;
        color: #606266;
        font-weight: 500;
      }
      
      .el-pagination__sizes {
        margin-right: 16px;
        
        .el-select .el-input {
          width: 110px;
          
          .el-input__wrapper {
            background-color: #f5f7fa;
            border-radius: 20px;
          }
        }
      }
      
      button {
        background-color: #f5f7fa;
        border-radius: 4px;
        margin: 0 3px;
        color: #606266;
        transition: all 0.2s ease;
        
        &:hover {
          color: #1890ff;
          background-color: #e6f7ff;
        }
        
        &:disabled {
          color: #c0c4cc;
        }
      }
      
      .btn-prev, .btn-next {
        padding: 0 8px;
        
        &:hover {
          background-color: #e6f7ff;
        }
      }
      
      .el-pager {
        li {
          background-color: #f5f7fa;
          border-radius: 4px;
          margin: 0 3px;
          color: #606266;
          font-weight: 500;
          min-width: 32px;
          transition: all 0.2s ease;
          
          &:hover {
            color: #1890ff;
            background-color: #e6f7ff;
          }
          
          &.is-active {
            background-color: #1890ff;
            color: #fff;
            font-weight: 600;
            box-shadow: 0 2px 6px rgba(24, 144, 255, 0.2);
            
            &:hover {
              background-color: #40a9ff;
            }
          }
        }
      }
      
      .el-pagination__jump {
        margin-left: 16px;
        color: #606266;
        
        .el-input__wrapper {
          background-color: #f5f7fa;
          border-radius: 4px;
        }
      }
    }
  }
  
  .change-preview {
    margin-bottom: 20px;
    padding: 16px;
    background-color: #f8faff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);

    h4 {
      margin-top: 0;
      margin-bottom: 12px;
      color: #1890ff;
      font-weight: 600;
      font-size: 16px;
      display: flex;
      align-items: center;
      
      &::before {
        content: '';
        display: inline-block;
        width: 4px;
        height: 16px;
        background: #1890ff;
        border-radius: 2px;
        margin-right: 8px;
      }
    }
    
    p {
      margin: 8px 0;
      line-height: 1.6;
      color: #606266;
      
      strong {
        color: #303133;
        margin-right: 4px;
      }
    }
  }
  
  :deep(.process-dialog) {
    border-radius: 12px;
    overflow: hidden;
    
    .el-dialog__header {
      padding: 16px 20px;
      border-bottom: 1px solid #f0f0f0;
      margin: 0;
      
      .el-dialog__title {
        font-size: 18px;
        font-weight: 600;
        color: #303133;
      }
    }
    
    .el-dialog__body {
      padding: 24px;
    }
    
    .el-dialog__footer {
      padding: 16px 20px;
      border-top: 1px solid #f0f0f0;
    }
    
    .el-form-item__label {
      font-weight: 600;
      color: #606266;
    }
    
    .status-group {
      .el-radio {
        margin-right: 20px;
        padding: 6px 10px;
        border-radius: 6px;
        transition: all 0.3s;
        
        &.is-checked {
          background-color: #e6f7ff;
        }
        
        &:hover {
          background-color: #f0f7ff;
        }
        
        .el-radio__input.is-checked .el-radio__inner {
          background-color: #1890ff;
          border-color: #1890ff;
        }
        
        .el-radio__input.is-checked + .el-radio__label {
          color: #1890ff;
          font-weight: 500;
        }
      }
    }
    
    .form-textarea {
      .el-textarea__inner {
        border-radius: 8px;
        padding: 10px 12px;
        min-height: 120px !important;
        box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1) inset;
        transition: all 0.3s;
        
        &:hover, &:focus {
          box-shadow: 0 0 0 1px #1890ff inset;
        }
      }
    }
    
    .dialog-footer {
      .el-button {
        border-radius: 8px;
        padding: 8px 20px;
        font-weight: 500;
        transition: all 0.3s;
        
        .el-icon {
          margin-right: 4px;
        }
        
        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
      }
      
      .submit-btn {
        background: linear-gradient(135deg, #1890ff, #36a6ff);
        border-color: #1890ff;
        
        &:hover {
          background: linear-gradient(135deg, #40a9ff, #5cbbff);
          border-color: #40a9ff;
        }
      }
    }
  }
}
</style>
